package com.easonji.admin.manager.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.easonji.admin.dao.PermissionDAO;
import com.easonji.admin.manager.PermissionManager;
import com.easonji.admin.mapper.PermissionMapper;
import com.easonji.admin.model.Permission;
import com.easonji.common.constant.Consts;
import com.easonji.common.constant.cache.CacheConsts;
import com.easonji.common.constant.cache.CacheKeyConsts;
import com.easonji.common.node.MenuNode;
import com.easonji.common.node.TreeNode;
import com.easonji.common.util.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: PermissionManagerImpl
 * @Description: 权限通用类实现
 * @Author: LiuJun
 * @date: 2018/07/31 13:02
 * @Version: 1.0
 * @Copyright: Copyright (c) 2018 - 2019
 */
@Service
public class PermissionManagerImpl implements PermissionManager {

    @Resource
    private PermissionMapper permissionMapper;

    @Resource
    private PermissionDAO permissionDAO;

    @Override
    public List<MenuNode> queryMenuNode(Integer appId, List<Integer> roleIds) {
        List<MenuNode> menuNodes = permissionDAO.selectMenuNodesByRoleIds(appId, roleIds);
        // 如果是权限管理系统删除按钮菜单
        boolean clearBtn = false;
        if (StringUtils.isNotEmpty(appId) && appId == Consts.DEFAULT_APP_ID) {
            clearBtn = true;
        }
        return MenuNode.buildTitle(clearBtn, menuNodes);
    }

    @Override
    @Cacheable(value = CacheConsts.CONSTS, key = "'" + CacheKeyConsts.PERMISSION_NAME + "'+#permissionId")
    public String getPermissionNameById(Integer permissionId) {
        String result = "";
        Permission param = new Permission();
        param.setId(permissionId);
        Permission permission = permissionMapper.selectOne(param);
        if (StringUtils.isNotEmpty(permission)) {
            result = permission.getName();
        }
        return result;
    }

    @Override
    @Cacheable(value = CacheConsts.CONSTS, key = "'" + CacheKeyConsts.PERMISSION_NAMES + "'+#permissionIds")
    public String getPermissionNameByIds(String permissionIds) {
        String[] ids = StringUtils.split(permissionIds, Consts.COMMA);
        Wrapper<Permission> wrapper = new EntityWrapper<>();
        wrapper.in("id", ids);
        List<Permission> permissions = permissionMapper.selectList(wrapper);
        if (StringUtils.isEmpty(permissions)) {
            return "";
        }
        StringBuilder result = new StringBuilder();
        permissions.forEach(permission -> {
            result.append(Consts.COMMA);
            result.append(permission.getName());
        });
        return result.substring(1);
    }

    @Override
    public List<String> queryPermissionsByRoleIds(List<Integer> roleIds) {
        return permissionDAO.selectPermissionByRoleIds(roleIds);
    }

    @Override
    public List<TreeNode> queryPermissionTreeNodeByAppId(Integer appId) {
        List<TreeNode> result = permissionDAO.selectPermissionTreeNodesByAppId(appId);
        result.add(TreeNode.createParent());
        return result;
    }

    @Override
    public List<TreeNode> queryPermissionTreeNodeByRoleId(Integer roleId) {
        return permissionDAO.selectPermissionTreeNodesByRoleId(roleId);
    }

    @Override
    public String getAppNameById(Integer permissionId) {
        return permissionDAO.selectAppNameById(permissionId);
    }
}
